<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../../plugins/element-ui/index.css"/>
    <link rel="stylesheet" href="../../styles/common.css"/>
    <link rel="stylesheet" href="../../styles/page.css"/>
</head>
<body>
<div class="dashboard-container" id="category-app">
    <div class="container">
        <div
                class="tableBar"
                style="display: inline-block"
        >
            <el-button
                    type="primary"
                    class="continue"
                    @click="addClass('class')"
            >
                + 新增题目
            </el-button>
            <el-input v-model="input" placeholder="请输入题目名称" style="width: 250px" clearable
                      @keyup.enter.native="handleQuery">
                <i slot="prefix" class="el-input__icon el-icon-search" style="cursor: pointer" @click="handleQuery"></i>
            </el-input>
        </div>
        <el-table
                :data="tableData"
                stripe
                class="tableBox"
        >
            <el-table-column
                    prop="id"
                    label="题目id"
            />
            </el-table-column>
            <el-table-column
                    prop="name"
                    label="题目名称"
            >
            </el-table-column>
            <el-table-column
                    prop="level"
                    label="等级"
            >
            </el-table-column>
            <el-table-column
                    prop="star"
                    label="星级"
            >
            </el-table-column>
            <el-table-column
                    prop="cover"
                    label="封面"
                    align="center"
            />
            <template slot-scope="{ row }">
            <el-image style="width: auto; height: 40px; border:none;cursor: pointer;"
                      :src="row.cover">
                <div slot="error" class="image-slot">
                    <img src="./../../images/noImg.png" style="width: auto; height: 40px; border:none;">
                </div>
            </el-image>
            </template>
            </el-table-column>
            <el-table-column label="操作" width="160" align="center">
                <template slot-scope="scope">
                    <el-button type="text" size="small" class="blueBug" @click="editHandle(scope.row.id)">查看问题</el-button>
                    <el-button type="text" size="small" class="delBut non" @click="deleteHandle(scope.row.id)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
                class="pageList"
                :page-sizes="[10, 20, 30, 40]"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="counts"
                @size-change="handleSizeChange"
                :current-page.sync="page"
                @current-change="handleCurrentChange"
        ></el-pagination>
    </div>
    <el-dialog :title="classData.title" :visible.sync="classData.dialogVisible" width="30%" :before-close="handleClose">
        <el-form class="demo-form-inline" label-width="100px">
            <el-form-item label="题目名称：">
                <el-input v-model="classData.name" placeholder="请输入题目名称" maxlength="14"/>
            </el-form-item>
            <el-form-item label="等级：">
                <el-input v-model="classData.level" placeholder="请输入等级"/>
            </el-form-item>
            <el-form-item label="星级：">
                <el-input v-model="classData.star" type="number" placeholder="请输入星级"/>
            </el-form-item>
            <el-form-item label="题目封面:" prop="region" class="uploadImg">
                <el-upload class="avatar-uploader" action="/common/upload"
                           :show-file-list="false"
                           :on-success="handleAvatarSuccess"
                           :on-change="onChange"
                           ref="upload">
                    <img v-if="imageUrl" :src="imageUrl" class="avatar"></img>
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
        <el-button size="medium" @click="classData.dialogVisible = false">取 消</el-button>
        <el-button type="primary" size="medium" @click="submitForm()">确 定</el-button>
        <el-button
                v-if="action != 'edit'"
                type="primary"
                size="medium"
                class="continue"
                @click="submitForm('go')"
        > 保存并继续添加 </el-button>
      </span>
    </el-dialog>
</div>
<script src="../../plugins/vue/vue.js"></script>
<script src="../../plugins/element-ui/index.js"></script>
<script src="../../plugins/axios/axios.min.js"></script>
<script src="../../js/request.js"></script>
<script src="../../api/category.js"></script>
<script>
    var imageUrl;
    new Vue({
        el: '#category-app',
        data() {
            return {
                input: '',
                action: '',
                counts: 0,
                page: 1,
                pageSize: 10,
                id: '',
                tableData: [],
                type: '',
                checkList: [],
                classData: {
                    'title': '添加题目',
                    'dialogVisible': false,
                    'name': '',
                    'level': '',
                    'star': '',
                    'imageUrl':''
                }
            }
        },
        computed: {},
        created() {
            this.init()
            this.user = JSON.parse(localStorage.getItem('userInfo')).username
        },
        mounted() {
        },
        methods: {
            async init() {
                const params={
                    page: this.page,
                    pageSize: this.pageSize,
                    name: this.input ? this.input : undefined//传递题目名
                }
                await getTestSoul(params).then(res => {
                    if (String(res.code) === '1') {
                        this.tableData = res.data.records || [];
                        this.counts=res.data.total
                    } else {
                        this.$message.error(res.msg || '操作失败')
                    }
                }).catch(err => {
                    this.$message.error('请求出错了：' + err)
                })
            },
            handleQuery() {
                this.page = 1;
                this.init();
            },
            // 全部操作
            handleSelectionChange(val) {
                let checkArr = []
                val.forEach((n) => {
                    checkArr.push(n.id)
                })
                this.checkList = checkArr
            },
            handleSizeChange(val) {
                this.pageSize = val
                this.init()
            },
            handleCurrentChange(val) {
                this.page = val
                this.init()
            },
            // 添加
            addClass(st) {
                if (st == 'class') {
                    this.classData.title = '新增菜品分类';
                    this.type = '1'
                } else {
                    this.classData.title = '新增套餐分类';
                    this.type = '2'
                }
                this.action = 'add';
                this.classData.name = '';
                this.classData.sort = '';
                this.classData.dialogVisible = true
            },
            editHandle(id) {
                location.href='/backend/question1.html?id='+id;
            },
            // 关闭弹窗
            handleClose(st) {
                this.classData.dialogVisible = false
            },
            //删除
            deleteHandle(id) {
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    'confirmButtonText': '确定',
                    'cancelButtonText': '取消',
                    'type': 'warning'
                }).then(() => {
                    deleteTest(id).then(res => {
                        if (res.code === 1) {
                            this.$message.success('删除成功！');
                            this.handleQuery()
                        } else {
                            this.$message.error(res.msg || '操作失败')
                        }
                    }).catch(err => {
                        this.$message.error('请求出错了：' + err)
                    })
                })
            },
            handleAvatarSuccess(response, file, fileList) {
                // 拼接down接口预览
                    imageUrl = response.data;
            },
            onChange(file) {
                if (file) {
                    const suffix = file.name.split('.')[1];
                    const size = file.size / 1024 / 1024 < 2;
                    if (['png', 'jpeg', 'jpg'].indexOf(suffix) < 0) {
                        this.$message.error('上传图片只支持 png、jpeg、jpg 格式！');
                        this.$refs.upload.clearFiles();
                        return false
                    }
                    if (!size) {
                        this.$message.error('上传文件大小不能超过 2MB!');
                        return false
                    }
                    return file;
                }
            },
            //数据提交
            submitForm() {
                const classData = this.classData;
                const valid = (classData.name === 0 || classData.name) && (classData.sort === 0 || classData.sort)
                if (this.action === 'add') {
                    addTest({
                                'name': classData.name,
                                'level': classData.level,
                                'star':classData.star,
                                'cover':imageUrl
                            }).then(res => {
                                if (res.code === 1) {
                                    this.$message.success('分类添加成功！')
                                    this.classData.dialogVisible = false
                                    this.handleQuery()
                                    if (!st) {
                                        this.classData.dialogVisible = false
                                    } else {
                                        this.classData.name = ''
                                        this.classData.sort = ''
                                    }
                                    this.handleQuery()
                                } else {
                                    this.$message.error(res.msg || '操作失败')
                                }
                            })

                }
            },
        }
    })
</script>
</body>
</html>